//
//  IAARiskMonitorAlarmVC.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/7.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAARiskMonitorAlarmVC.h"
#import "IAARiskMonAlarmTCell.h"
#import "IAAInfoMsgModel.h"

@interface IAARiskMonitorAlarmVC ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) IAARiskMonitorModel *model;

@end

static NSString * const reuseCellId = @"IAARiskMonAlarmTCell";

@implementation IAARiskMonitorAlarmVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //设置导航栏
    [self initNavigationBar];
    //設置子Views
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initNavigationBar {
    self.navigationItem.title = @"风险预警信息";
}

- (void)initSubViews {
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //    [self.tableView registerClass:[IAAAuthCheckTHeader class] forHeaderFooterViewReuseIdentifier:reuseHeaderId];
    UINib *cellNib = [UINib nibWithNibName:reuseCellId bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:reuseCellId];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self requestRiskMonitorInfo];
}

#pragma mark - <UITableViewDelegate>代理
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UILabel *label = [UILabel new];
//    label.text = @"风险检测提示，请重点审核以下事项：";
//    label.textColor = IAA_COLOR_0x129CFF(1.0);
//    return label;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - <UITableViewDataSource>代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.infolist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IAARiskMonAlarmTCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCellId forIndexPath:indexPath];
    [cell configViewWithData:self.model index:indexPath.row];
    return cell;
}

#pragma mark - 网络请求
- (void)requestRiskMonitorInfo {
    NSString *urlStr = [IAAUrlProtocolMerge appRiskMonitorInfo];
    NSDictionary *pars = @{@"token"  : IAA_APP_DELEGATE.token,
                           @"busiId" : self.busiId,
                           @"osName" : @"iOS",
                           @"infoNo" : (self.infoNo ? self.infoNo : @"")};
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [XDAFHttpRequest POST:urlStr parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"风险预警信息请求返回%@", responseObject);
        [hud hide:YES];
        self.model = [IAARiskMonitorModel yy_modelWithJSON:responseObject];
        if ([self.model.resultCode isEqualToString:IAA_RESULT_CODE_SUC]) {
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showPromptWithAutoHideInView:self.view message:self.model.resultMsg];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"风险预警信息请求失败%@", error);
        [hud hide:NO];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"网络请求失败"];
    }];
}

@end
